home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / grafica / amhelios / ff_clip.cpp < prev    next >
C/C++ Source or Header  |  1999-01-01  |  4KB  |  158 lines

  1. ////////////////////////////////////////////////////////////
  2. //
  3. //  FF_CLIP.CPP - Form Factor Polygon Clipper Class
  4. //
  5. //  Version:    1.03A
  6. //
  7. //  History:    94/08/23 - Version 1.00A release.
  8. //              94/12/16 - Version 1.01A release.
  9. //              95/02/05 - Version 1.02A release.
  10. //              95/07/21 - Version 1.02B release.
  11. //              96/02/14 - Version 1.02C release.
  12. //              96/04/01 - Version 1.03A release.
  13. //
  14. //  Compilers:  Microsoft Visual C/C++ Professional V1.5
  15. //              Borland C++ Version 4.5
  16. //
  17. //  Author:     Ian Ashdown, P.Eng.
  18. //              byHeart Software Limited
  19. //              620 Ballantree Road
  20. //              West Vancouver, B.C.
  21. //              Canada V7S 1W3
  22. //              Tel. (604) 922-6148
  23. //              Fax. (604) 987-7621
  24. //
  25. //  Copyright 1994-1996 byHeart Software Limited
  26. //
  27. //  The following source code has been derived from:
  28. //
  29. //    Ashdown, I. 1994. Radiosity: A Programmer's
  30. //    Perspective. New York, NY: John Wiley & Sons.
  31. //
  32. //  It may be freely copied, redistributed, and/or modified
  33. //  for personal use ONLY, as long as the copyright notice
  34. //  is included with all source code files.
  35. //
  36. ////////////////////////////////////////////////////////////
  37.  
  38. #include "ff_clip.h"
  39.  
  40. // Clip element
  41. int FormClip::Clip( Element3 *pelem, FormPoly &out, WORD
  42.     poly_id )
  43. {
  44.   int i;            // Loop index
  45.   int num_vert;     // Number of vertices
  46.   Vertex3 *pvert;   // 3-D world space vertex pointer
  47.   Vector4 hv;       // 4-D homogeneous co-ord vertex
  48.  
  49.   out.Reset(poly_id);   // Reset output polygon
  50.  
  51.   num_vert = pelem->GetNumVert();
  52.   for (i = 0; i < num_vert; i++)
  53.   {
  54.     // Get world space vertex position pointer
  55.     pvert = pelem->GetVertexPtr(i);
  56.  
  57.     // Set homogeneous co-ordinates vertex
  58.     hv.ProjTransform(pvert->GetPosn(), vtm);
  59.  
  60.     pclip->Clip(hv, out);       // Clip polygon edge
  61.   }
  62.  
  63.   pclip->Close(out);    // Close polygon
  64.       
  65.   return out.GetNumVert();
  66. }
  67.  
  68. // Output view space vertex
  69. void FormClipEdge::Output( Vector4 &v, FormPoly &out )
  70. {
  71.   if (pnext != NULL)    // More planes ?
  72.     pnext->Clip(v, out);
  73.   else
  74.     out.AddVertex(v);
  75. }
  76.  
  77. // Calculate intersection vertex
  78. Vector4 FormClipEdge::Intersect( Vector4 &s, Vector4 &e )
  79. {
  80.   double d, t;  // Temporary variables
  81.   Vector4 r;    // Temporary vector
  82.  
  83.   // Calculate parameter
  84.   r = (e - s);
  85.   d = Dot(normal, r);
  86.  
  87.   if (fabs(d) > MIN_VALUE)
  88.     t = -Dot(normal, s) / d;
  89.   else
  90.     t = 1.0;
  91.  
  92.   if (t < 0.0)  // Ensure lower limit
  93.     t = 0.0;
  94.  
  95.   if (t > 1.0)  // Ensure upper limit
  96.     t = 1.0;
  97.  
  98.   // Calculate intersection vertex co-ordinates
  99.   r *= t;
  100.  
  101.   return (s + r);
  102. }
  103.  
  104. // Clip polygon edge
  105. void FormClipEdge::Clip( Vector4 ¤t, FormPoly &out )
  106. {
  107.   BOOL curr_inside;     // Current point inside flag
  108.   Vector4 isect;        // Intersection vertex
  109.  
  110.   // Determine vertex visibility
  111.   curr_inside = IsInside(current);
  112.  
  113.   if (first_flag == FALSE)      // First vertex seen ?
  114.   {
  115.     first = current;
  116.     first_inside = curr_inside;
  117.     first_flag = TRUE;
  118.   }
  119.   else
  120.   {
  121.     // Does edge intersect plane ?
  122.     if (start_inside ^ curr_inside)
  123.     {
  124.       isect = Intersect(start, current);
  125.       Output(isect, out);
  126.     }
  127.   }
  128.  
  129.   if (curr_inside == TRUE)
  130.     Output(current, out);
  131.  
  132.   start = current;
  133.   start_inside = curr_inside;
  134. }
  135.  
  136. // Close polygon
  137. void FormClipEdge::Close( FormPoly &out )
  138. {
  139.   Vector4 isect;        // Intersection vertex
  140.  
  141.   if (first_flag == TRUE)
  142.   {
  143.     // Does edge intersect plane ?
  144.     if (start_inside ^ first_inside)
  145.     {
  146.       isect = Intersect(start, first);
  147.       Output(isect, out);
  148.     }
  149.  
  150.     if (pnext != NULL)  // More planes ?
  151.       pnext->Close(out);
  152.  
  153.     // Reset first vertex seen flag
  154.     first_flag = FALSE;
  155.   }
  156. }
  157.  
  158.